package com.example.test_load_pic_rough.listener;

import android.os.Handler;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.example.test_load_pic_rough.adapter.UserListAdapter;
import com.example.test_load_pic_rough.adapter.ViewListAdapter;
import com.example.test_load_pic_rough.bean.User;

import java.util.Collections;

/**
 * 任意的view都可绑定一个dragListener，绑定之后可以捕捉到 拖拽的状态
 *
 * @Override public boolean onDrag(View v, DragEvent event) {
 * switch (event.getAction()) {
 * case DragEvent.ACTION_DRAG_STARTED:
 * // 拖拽开始时处理逻辑
 * break;
 * case DragEvent.ACTION_DRAG_ENTERED:
 * // 拖拽进入视图时处理逻辑
 * break;
 * case DragEvent.ACTION_DRAG_LOCATION:
 * // 拖拽过程中（位置改变时）处理逻辑
 * break;
 * case DragEvent.ACTION_DRAG_EXITED:
 * // 拖拽离开视图时处理逻辑
 * break;
 * case DragEvent.ACTION_DROP:
 * // 拖拽放下时处理逻辑
 * // 可以调用 event.getLocalX() 和 event.getLocalY() 获取放下位置
 * // 可以调用 event.getClipData() 获取拖拽的数据
 * break;
 * case DragEvent.ACTION_DRAG_ENDED:
 * case DragEvent.ACTION_DRAG_CANCELED:
 * // 拖拽结束或取消时处理逻辑
 * break;
 * default:
 * break;
 * }
 * return true; // 表示我们已经处理了该事件
 * }
 */
public class RecycleViewOnDragListener implements View.OnDragListener {
    private String TAG = getClass().getSimpleName();

    private Handler mHandler = new Handler();

    /**
     * @param view      拖拽结束后落在了那个view上面
     * @param dragEvent
     * @return
     */
    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        switch (dragEvent.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                Log.e(TAG, "onDrag: 开始拖拽");
            case DragEvent.ACTION_DROP: //拖拽放下时
//            case DragEvent.ACTION_DRAG_LOCATION:
                Log.e(TAG, "onDrag: 停止拖拽");
                handleDrag(view, dragEvent);
                break;
        }
        return true;
    }

    //view -- 拖拽结束后落所落在的view上
    private void handleDrag(View view, DragEvent dragEvent) {
        //如果拖拽的对象不是Recyclerview，不处理，
        RecyclerView endRecycleView = null; //拖拽到的rv
        if (view instanceof RecyclerView) {
            endRecycleView = (RecyclerView) view;
        } else {
            return;
        }
        //通过拖拽的点的x y  找到当前拖拽的是那个item
        View item = endRecycleView.findChildViewUnder(dragEvent.getX(), dragEvent.getY());
        //找不到就不处理
        if (item == null) {
            return;
        }
        //拿到被拖拽对象的信息
        DragData dragData = (DragData) dragEvent.getLocalState();
        //找到被拖拽的item在原来的rv中的position
        int fromPosition = dragData.viewHolder.getLayoutPosition();
        Log.e(TAG, "在原来Recyclerview中的position=>" + fromPosition);
        //找到拖拽进入到了目标rv中的那个item的position
        int toPosition = endRecycleView.getChildLayoutPosition(item);
        Log.e(TAG, "落在新的Recyclerview中的position=>" + toPosition);
        //出现异常停止处理
        if (fromPosition < 0 || toPosition < 0) {
            return;
        }


        // 视频窗口之间换位置，视频宫格之间拖动
        if (dragData.recyclerView == endRecycleView) { //如果最后落在了原始的Recyclerview中
            // 同一个RecycleView中
            Log.e(TAG, "handleDrag: 同一个RecycleView中" + " fromPosition=" + fromPosition + " toPosition=" + toPosition);
            if (fromPosition == toPosition) {
                return;
            }

            if (endRecycleView.getAdapter() instanceof ViewListAdapter) {
                ViewListAdapter endAdapter = (ViewListAdapter) endRecycleView.getAdapter();
                //User user = endAdapter.getmList().get(toPosition);

                //交换集合中两个位置的数据
                Collections.swap(endAdapter.getmList(), fromPosition, toPosition);
                if (fromPosition < toPosition) {
                    endAdapter.notifyItemMoved(fromPosition, toPosition); //这里move之后，原来位置的item，被往后放了一个
                    endAdapter.notifyItemMoved(toPosition - 1, fromPosition); //所以把原来的那个item，放到fromPosition
                } else {
                    endAdapter.notifyItemMoved(fromPosition, toPosition);
                    endAdapter.notifyItemMoved(toPosition + 1, fromPosition);
                }
            }
        } else if (endRecycleView.getAdapter() instanceof ViewListAdapter) { //如果落在了其他的Recyclerview中
            Log.e(TAG, "handleDrag: 不同Recyclerview之间交换数据");
            ViewListAdapter endAdapter = (ViewListAdapter) endRecycleView.getAdapter();
            //拿到enRv中item中的数据
            User tData = endAdapter.getmList().get(toPosition);
            Log.e(TAG, "handleDrag: endRv中原来的数据" + tData);

            UserListAdapter fromAdapter = (UserListAdapter) dragData.recyclerView.getAdapter();
            User fData = fromAdapter.getmList().get(fromPosition);

            Log.e(TAG, "原来数据" + fData + "=>" + tData);

            endAdapter.getmList().set(toPosition, fData);
            endAdapter.notifyItemChanged(toPosition);

        }

    }

    public static void startDrag(RecyclerView recyclerView, RecyclerView.ViewHolder vh) {
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(vh.itemView);
        vh.itemView.startDrag(null,
                shadowBuilder,
                new DragData(recyclerView, vh),
                0);
    }

    public static class DragData {

        //当前拖拽的是那个rv中的item
        public RecyclerView recyclerView;
        //拖拽的item的viewholder
        public RecyclerView.ViewHolder viewHolder;

        public DragData(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            this.recyclerView = recyclerView;
            this.viewHolder = viewHolder;
        }
    }

}
